<?php
class ControllerFtp extends Controller {
	public function __construct($q) {
		parent::__construct($q);
	}
	
	public function execute() {
		$v = mb_strtolower($this->getQuery());

		if(empty($v))
			return $this->doHelp();

		$c = 'do'.ucfirst($v);
		if(method_exists($this, $c))
			$this->$c();
		else
			throw new SignalArgUnknown($v);
	}

	private function doHelp() {
		View::show("Usage : ftp command [options]");
	}

	private function doList() {
		if(!ModelUser::getInstance())
			return View::show("[-] You are not logged in"); 
		View::show("[+] Domains list");
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,
				"https://clients.1and1.fr/xml/config/DomainOverview;jsessionid=".ModelConfig::get('sessionid'));
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_COOKIEJAR, ModelConfig::get('cookie'));
		curl_setopt($ch, CURLOPT_COOKIEFILE, ModelConfig::get('cookie'));
		$init = curl_exec($ch);
		
		preg_match_all('/domainOverview.DomainIds=([0-9]+)[^>]+>([^<]+)+/', $init, $m, PREG_SET_ORDER);
		foreach($m as $v)
			View::show("\t[+] {$v[1]}\t{$v[2]}");
	}
}